//
//  ApplySelectAreaController.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/10/12.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "ApplySelectAreaController.h"
#import "StoreCityModel.h"

@interface ApplySelectAreaController ()
@property (weak, nonatomic) IBOutlet UILabel *provinceLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;

@property (weak, nonatomic) IBOutlet UIView *areaContent;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *areaContentHeight;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) StoreProvinceModel *provinceModel;
@property (nonatomic, strong) StoreCityModel *cityModel;
@property (nonatomic, strong) YCAreaModel *areaModel;

@end

@implementation ApplySelectAreaController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择地址";
    
    [self requestProvince];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setTitleColor:[UIColor colorWithHexString:@"323232"] forState:UIControlStateNormal];
    btn.titleLabel.font = lbFont(15);
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void) confirmAction {
    
    NSString *address = [NSString stringWithFormat:@"%@%@%@", self.provinceModel.name ?: @"", self.cityModel.name ?: @"", self.areaModel.name ?: @""];
    self.selectBlock(self.provinceModel.ID, self.cityModel.ID, self.areaModel.ID, address);
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)provinceAction:(UIButton *)sender {
    
    self.cityModel = nil;
    self.areaModel = nil;
    
    [self requestProvince];
}
- (IBAction)cityAction:(UIButton *)sender {
    
    self.areaModel = nil;
    
    [self requestCity];
}
- (IBAction)areaAction:(UIButton *)sender {
    
    [self requestArea];
}

- (void)setDataArray:(NSArray *)dataArray {
    
    _dataArray = dataArray;
    
    for (UIView *view in self.areaContent.subviews)
    {
        [view removeFromSuperview];
    }
    
    CGFloat margin = 0;
    CGFloat W = (lbScreenWidth - 5 * margin) / 4.0f;
    CGFloat H = 35;
    CGFloat X = 0;
    CGFloat Y = 0;
    
    NSInteger line = 0;
    NSInteger queue = 0;
    int i = 0;
    for (id model in _dataArray)
    {
        
        X = queue * (W + margin) + margin;
        Y = line * (H + margin) + margin;
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, W, H)];
        
        NSString *title = @"";
        if ([model isKindOfClass:StoreProvinceModel.class])
        {
            title = [(StoreProvinceModel *)model name];
        }
        else if ([model isKindOfClass: StoreCityModel.class])
        {
            title = [(StoreCityModel *)model name];
        }
        else if ([model isKindOfClass:YCAreaModel.class])
        {
            title = [(YCAreaModel *)model name];
        }
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"323232"] forState:UIControlStateNormal];
        btn.titleLabel.font = lbFont(15);
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(selectAreaAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.areaContent addSubview:btn];
        
        queue++;
        i++;
        if (queue % 4 == 0)
        {
            queue = 0;
            line++;
        }
    }
    
    _areaContentHeight.constant = (line + 1) * (H + margin) + margin;
    
}

- (void) selectAreaAction:(UIButton *) sender {
    
    NSInteger index = sender.tag - 100;
    
    id model = self.dataArray[index];
    
    if ([model isKindOfClass:StoreProvinceModel.class])
    {
        self.provinceModel = model;
    }
    else if ([model isKindOfClass: StoreCityModel.class])
    {
        self.cityModel = model;
    }
    else if ([model isKindOfClass:YCAreaModel.class])
    {
        self.areaModel = model;
    }
    
}

- (void)setProvinceModel:(StoreProvinceModel *)provinceModel {
    _provinceModel = provinceModel;

    _provinceLabel.text = _provinceModel.name ?: @"省";
    if(_provinceModel) [self requestCity];
    
}

- (void)setCityModel:(StoreCityModel *)cityModel {
    _cityModel = cityModel;
    
    _cityLabel.text = _cityModel.name ?: @"市";
    if(_cityModel) [self requestArea];
}

- (void)setAreaModel:(YCAreaModel *)areaModel {
    _areaModel = areaModel;
    _areaLabel.text = _areaModel.name ?: @"区";
}

- (void) requestProvince {
    
    [LBNetworkingInstance requestGET:getAreaInfo parameters:@{@"level":@"1"} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:StoreProvinceModel.class json:responseObject[@"Data"]];
            
            self.dataArray = array;
        }
        
    } failure:^(NSError *err) {
        
    }];
}

- (void) requestCity {
    
    NSString *pid = self.provinceModel.ID;
    NSString *level = @"2";
    if (!pid)
    {
        [MBProgressHUD showMessageWithOutIcon:@"请选择省"];
        return;
    }
    
    [LBNetworkingInstance requestGET:getAreaInfo parameters:NSDictionaryOfVariableBindings(pid,level) showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:StoreCityModel.class json:responseObject[@"Data"]];
            
            self.dataArray = array;
        }
        
    } failure:^(NSError *err) {
        
    }];
}

- (void) requestArea {
    
    NSString *pid = self.cityModel.ID;
    NSString *level = @"3";
    if (!pid)
    {
        [MBProgressHUD showMessageWithOutIcon:@"请选择城市"];
        return;
    }
    
    
    [LBNetworkingInstance requestGET:getAreaInfo parameters:NSDictionaryOfVariableBindings(pid,level) showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:YCAreaModel.class json:responseObject[@"Data"]];
            
            self.dataArray = array;
        }
        
    } failure:^(NSError *err) {
        
    }];
}

@end
